<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="author" content="Jxm">
    <script src="../jQuery/jquery-3.1.1.js"></script>
    <title>星星评分</title>
        <style>
         body{
             font: 12px/1.5 arial;
             color: #666;
         }   
         ul,p{
             margin: 0;
             padding: 0;
         }
         #star{
             width: 600px;
             margin: 10px auto;
             position: relative;
         }
         #star span,
         #star ul{
             float: left;
             margin: 0 5px;
         }
         #star li{
             list-style: none;
             float: left;
             width: 24px;
             height: 24px;
             cursor: pointer;
             background-image: url(img/star.png);
             background-repeat: no-repeat;
             margin: auto 5px;
         }
         #star li.on{
             background-position: 0 -28px;
         }
         #star p{
             position: absolute;
             top: 20px;
             width: 160px;
         }
         #star p em{
             display: block;
             font-style: normal;
             color: #f60;
         }
         #star strong{
             color: #f60;
         }
    </style>
</head>

<body>
    <div id="star">
        <span>点击星星评分</span>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <span></span>
            <p></p>
        </ul>
    </div>
</body>

</html>

<script>

    var msg=[
        "很不满意|差得太离谱，与卖家描述的严重不符，非常不满",
        "不满意|部分有破损，与卖家描述的不符，不满意",
        "一般|质量一般，没有卖家描述的那么好",
        "满意|质量不错，与卖家描述的基本一致，还是挺满意的",
        "非常满意|质量非常好，与卖家描述的完全一致，非常满意"
    ]

    var isStar=0;
    $('li').each(function(){
        $('li').mouseover(function(){
            var index=$('li').index($(this))
            showStar(index+1)
            $('p').text((index+1)+msg[index].split('|')[0]+msg[index].split('|')[1])
            var left = $('ul').get(0).offsetLeft+$('ul li').get(0).offsetWidth*index;
            $('p').css('left',left+'px');
        })
        $('li').click(function(){
            var index = $('li').index($(this));
            isStar = index+1;
            $('p').text('');
            $('ul span').text(isStar+'分('+msg[index].split('|')[1]+')')
        })
        $('li').mouseout(function(){
            showStar(isStar)
            $('p').text('');
        })
    })
    function showStar(index){
        $('li').each(function(){
            if($('li').index($(this))<index){
                $(this).addClass('on')
            }else{
                $(this).removeClass('on')                
            }
        })
    }



</script>